<?php

// Model:'CoSoThanhTra' - Database Table: 'cs_tt'

Class CoSoThanhTra extends Eloquent {

    //Cac bang tham gia trong model
    private $tables = array("cs_tt", "chi_tiet_cs_tt");
    //Rang buoc du lieu
    private $rules = array(
        "MST" => "required|min:5|max:15",
        "TEN_CS" => "required|min:1|max:200",
        "CHU_CS" => "required|min:1|max:200",
        "GIAY_CN_SO" => "required|min:1|max:10",
        "NOI_CAP" => "required|min:1|max:50",
        "NGAY_CAP" => "required",
        "DIA_CHI" => "required|min:1|max:200",
    );
    //So co so hien thi tren mot trang
    private $cstt = 8;

    //Ham kiem tra du lieu
    public function validation($data) {
        return Validator::make($data, $this->rules);
    }

    //Lay so du lieu hien thi tren trang
    public function GetCSTT() {
        return $this->cstt;
    }

    //Lay ds du lieu
    public function GetList($key) {
        if ($key == null) {
            return DB::table($this->tables[1])
                            ->join($this->tables[0], function($join) {
                                $join->on($this->tables[0] . '.ID', '=', $this->tables[1] . '.ID_CS_TT');
                            })
                            ->select($this->tables[0] . ".ID", $this->tables[0] . ".MST", $this->tables[0] . ".TEN_CS", $this->tables[0] . ".CHU_CS", $this->tables[1] . ".NGANH_NGHE_KD", DB::raw($this->tables[1] . ".ID as ID_CT_CS_TT"))
                            ->paginate($this->cstt);
        }
        return DB::table($this->tables[1])
                        ->join($this->tables[0], function($join) {
                            $join->on($this->tables[0] . '.ID', '=', $this->tables[1] . '.ID_CS_TT');
                        })
                        ->select($this->tables[0] . ".ID", $this->tables[0] . ".MST", $this->tables[0] . ".TEN_CS", $this->tables[0] . ".CHU_CS", $this->tables[1] . ".NGANH_NGHE_KD", DB::raw($this->tables[1] . ".ID as ID_CT_CS_TT"))
                        ->where("MST", "LIKE", "%" . $key . "%")
                        ->orWhere("TEN_CS", "LIKE", "%" . $key . "%")
                        ->paginate($this->cstt);
    }
    
    //Lay du lieu tat ca
    public function GetListAll() {
        return DB::table($this->tables[0])
                        ->select("*")
                        ->orderBy("ID")
                        ->paginate($this->cstt);
    }

    //Lay du lieu xac dinh
    public function GetOne($id) {
        return DB::table($this->tables[0])
                        ->select("*")
                        ->where("ID", $id)
                        ->first();
    }

    //Lay du lieu xac dinh
    public function GetOneWithMST($mst) {
        return DB::table($this->tables[0])
                        ->select("*")
                        ->where("MST", $mst)
                        ->first();
    }

    //Luu du lieu moi
    public function SaveIn($data) {
        return DB::table($this->tables[0])
                        ->insertGetId($data);
    }

    //Luu du lieu sua doi
    public function StoreIn($data, $id) {
        try{
        DB::table($this->tables[0])
                        ->where("ID", $id)
                        ->update($data);
        return true;
        } catch (Exception $ex) {
        return false;
        }
    }

    //Xoa du lieu xac dinh
    public function Remove($id) {
        try {
            return DB::table($this->tables[0])
                            ->delete($id);
        } catch (Exception $ex) {
            return false;
        }
    }

}
